<template>
  <business-layout title='导出数据配置管理'>
    <div class='table-operator' slot='query'>
      <a-form :form='form1' layout='inline' @submit='handleQueryData'>
        <a-form-item label='分类名称'>
          <a-input placeholder='请输入分类名称' v-decorator='["keywords"]' />
        </a-form-item>
        <a-form-item>
          <a-button type='primary' html-type='submit'>
            <a-icon type='search' />
            查询
          </a-button>

          <a-button>
            重置
          </a-button>
        </a-form-item>
      </a-form>
    </div>

    <div slot='btns'>
      <a-button type='primary'>新增</a-button>
    </div>

    <s-table
      ref='table'
      size='default'
      rowKey='key'
      :columns='columns'
      :data='loadData'
      :alert='true'
      :rowSelection='rowSelection'
      showPagination='auto'
    >
      <span slot='serial' slot-scope='text, record, index'>
        {{ index + 1 }}
      </span>
      <span slot='description' slot-scope='text'>
        <ellipsis :length='4' tooltip>{{ text }}</ellipsis>
      </span>

      <span slot='action' slot-scope='text, record'>
        <template>
          <a @click='handleEdit(record)'>配置</a>
          <a-divider type='vertical' />
          <a @click='handleSub(record)'>订阅报警</a>
        </template>
      </span>
    </s-table>

    <create-form
      ref='createModal'
      :visible='visible'
      :loading='confirmLoading'
      :model='mdl'
      @cancel='handleCancel'
      @ok='handleOk'
    />
  </business-layout>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getRoleList, getServiceList } from '@/api/manage'

import CreateForm from './modules/CreateForm.vue'

const columns = [
  {
    title: '#',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '系统ID',
    dataIndex: 'no'
  },
  {
    title: '系统名称',
    dataIndex: 'description',
    scopedSlots: { customRender: 'description' }
  },
  {
    title: '设备ID',
    dataIndex: 'id1'
    // needTotal: true,
    // customRender: (text) => text + ' 次'
  },
  {
    title: '设备名称',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '点位ID',
    dataIndex: 'id2'
  },
  {
    title: '点位名称',
    dataIndex: 'point_name'
  },
  {
    title: '时间',
    dataIndex: 'updatedAt',
  },
  {
    title: '数据',
    dataIndex: 'callNo'
  }
]

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    CreateForm
  },
  data () {
    this.columns = columns
    return {
      activeKey: '1',
      // create model
      visible: false,
      confirmLoading: false,
      form1: this.$form.createForm(this),
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return new Promise((resolve, reject) => {
          resolve({
            endRow: 10,
            hasNextPage: false,
            hasPreviousPage: false,
            isFirstPage: false,
            isLastPage: false,
            list: [{}],
            navigateFirstPage: 0,
            navigateLastPage: 0,
            navigatePages: 0,
            navigatepageNums: null,
            nextPage: 0,
            pageNum: 1,
            pageSize: 10,
            pages: 3,
            prePage: 0,
            size: 10,
            startRow: 0,
            total: 1
          })
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {},

  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handleAdd () {
      this.mdl = null
      this.visible = true
    },
    handleEdit (record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleOk () {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (values.id > 0) {
            // 修改 e.g.
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('更新成功')
            })
          } else {
            // 新增
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSub (record) {
      if (record.status !== 0) {
        this.$message.success(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handleQueryData (e) {
      e.preventDefault()
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
<style lang='less' scoped>
.table-tab {

}
</style>
